import os
from bs4 import BeautifulSoup

# CURRENT_PATH = 'D:\\WORKPLACE\\jmeterTest\\Version1.0'


# demo先把路径写死

# begin采用时间自动获取
begin = "20200110_094242-"
CURRENT_PATH = os.getcwd ()
JMX_API_PATH = CURRENT_PATH + "\\api_jmx\\"
REPORT_PATH = CURRENT_PATH + "\\report\\" + begin + "report\\"
RESULT_PATH = CURRENT_PATH + "\\result\\" + begin + "result\\"
head = '<!DOCTYPE html><html lang="en"><head><title>报告汇总主页</title></head><body><div ' \
       'name="reportTable" style="text-align: center"><table border="1" style="margin: auto"> '
end = '</table></div></body></html>'

if __name__ == '__main__':
    soup = BeautifulSoup (open ("reportIndex.html", "r", encoding='UTF-8'), "html.parser")
    # print (soup.prettify ())  # 可以美化这段html并打印出来

    print ("报告文件夹是：", REPORT_PATH)

    # 获取报告文件夹内容列表
    for root, dirs, files in os.walk (REPORT_PATH):
        break  # 取第一子目录列表
    # 1.获取目录名
    print (dirs)

    # 整理内容
    # 设置标题
    title = ['执行时间', 'jmx文件', '响应结果', '报告详情']
    # title = ['执行时间', 'jmx文件', '报告详情']
    th_str = '<tr name="title">'  # 标题行
    for x in title:
        th_str = th_str + '<th>' + x + '</th>'
    else:
        th_str = th_str + '</tr>'

    th_soup = BeautifulSoup (th_str, "html.parser")
    print ("==>>【打印标题】\n", th_soup.prettify (), "\n================")
    # print ("【打印标题】", th_str)

    # 如何获取列表长度呢？
    length = len (dirs)
    print ("==>>文件夹长度（个数）：", length, "\n================")

    # 那么有问题了，同一个文件夹，把之前的报告也读了，要处理
    # 每次都新建文件夹，以起测试时间为文件名，包含这次所有报告

    # 2.构建链接,格式如下，进行地址拼接
    # D:\WORKPLACE\jmeterTest\Version1.0\report\20200108_165049_api3\index.html
    # REPORT_PATH + dirname + index.html(记得加斜杠分隔符）

    # 初始化列表主题行内容
    report_td = ''

    # 如何分割名称并且按第几个开始切，不管了，先把文件夹名字设为【date-api】，用-分割
    for foldername in dirs:
        report_td += '<tr>'
        print ("==>>文件夹名称:", foldername)
        # 1执行时间
        time, apiName = foldername.split ('-')
        print ("==>>>创建时间:", time)
        report_td += '<td>' + time + '</td>'
        # 2jmx文件
        print ("==>>>api名称:", apiName)
        report_td += '<td><a href="' + JMX_API_PATH + apiName + '.jmx" target="_blank">' + apiName + '</a></td>'
        print ("==>>api文件地址：", JMX_API_PATH + apiName)

        # 3响应结果jls
        report_td += '<td><a href="' + RESULT_PATH + time +'-'+ apiName + '_result.jsl" target="_blank">' + time+apiName + '_result.jsl</a></td>'
        print ("==>>jsl文件地址：", RESULT_PATH + time +'-'+ apiName + '_result.jsl')

        # 4报告详情
        report_td += '<td><a href="' + REPORT_PATH + foldername + '\\index.html" target="_blank">查看</a></td>'
        print ("==>>index.html文件地址：", REPORT_PATH + foldername + '\\index.html')
        report_td += "</tr>"

    # 3.插入每一个报告
    # 获取name属性为reportTable的div标签
    # print ('div:', soup.select ('div[name|=reportTable]'))
    div = soup.select ('div[name|=reportTable]')
    div.append (th_str + report_td)
    # print(div)

    print ("报告汇总在本次生成报告文件夹下面：", REPORT_PATH + begin + 'SummaryReport.html')
    htmldemo = open (REPORT_PATH + begin + 'SummaryReport.html', 'w')  # w-覆盖写，a-末尾追加写
    htmldemo.write (head + th_str + report_td + end)
    htmldemo.close ()
